package yuchen.androidocr;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import yuchen.androidocr.imageUtil.ImageThreshold;
import yuchen.androidocr.imageUtil.ImageUtils;
import yuchen.androidocr.imageUtil.sobelEdge;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView imageView;
    private Button mButton;
    //private ImageView imageView2;
    private Bitmap inImage;
    private TextView resultTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String sd_default = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        imageView = (ImageView) findViewById(R.id.imageView);
        mButton = (Button)findViewById(R.id.button);
        //imageView2 = (ImageView)findViewById(R.id.imageView2);
        resultTV = (TextView)findViewById(R.id.textView);
        String img_path = sd_default + "/img/44.9.png";
        Log.d("path:" ,img_path);
        inImage = BitmapFactory.decodeFile(img_path);
        imageView.setImageBitmap(inImage);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.button:
                sobelEdge sobelEdge = new sobelEdge(inImage.getWidth(),inImage.getHeight(),inImage);
                sobelEdge.readImage(inImage);
                int sobelData[] = sobelEdge.createEdgeImage();
                ImageThreshold imageThreshold = new ImageThreshold();
                //Bitmap binary_image = sobelEdge.createBitmap((int[])imageThreshold.OTSUAlgorithm(sobelData,inImage.getWidth(),inImage.getHeight()).get(0));
                int sobel[] = (int[])imageThreshold.OTSUAlgorithm(sobelData,inImage.getWidth(),inImage.getHeight()).get(0);

                ImageUtils imageUtils = new ImageUtils(sobel,inImage);
////                Bitmap result_image = sobelEdge.createBitmap(imageUtils.getNumberArea());
////                Bitmap binary_image2 = sobelEdge.createBitmap((int[])imageThreshold.OTSUAlgorithm(result_image).get(0));
//                //imageView2.setImageBitmap(binary_image2);
                resultTV.setText((Integer) imageUtils.getNumberArea().get(0));
        }
    }
}
